package VWorldGameStudio;


import FCRendering.ImageUtil;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;

import java.awt.*;
import java.util.ArrayList;

import static FCRendering.FCRMaster.fcv;

/**
 * bpb
 * 就是地图动画，放在地图上自动轮询的动画,地图动画的帧率都是固定的
 * 地图动画帧率是固定的，100毫秒一次
 */
public class FCMapAnimation extends Sprite{

    @Setter
    @Getter
    private String aName;

    @Setter
    @Getter
    private int mapLevel = 0;//等级，0 就是默认在地面，1就是玩家角色以及npc之上

    @Getter
    private ArrayList<String> animatImgUrls = new ArrayList<>(0);

    @Getter
    private ArrayList<Image> animatImgs = new ArrayList<>(0);

    @Setter
    @Getter
    private int visiable = 1;//默认显示

    private int showIndex = 0;

    @Getter
    private int imgSizeW;
    @Getter
    private int imgSizeH;

    @Getter
    private int needPlayTime = -1;//-1 就是无限，1就是一次

    private int curPlayTimes = 0;//当次播放次数

    public int getDrawImgSizeW(){
       return (int)(imgSizeW*fcv.getProportion());
    }

    public int getDrawImgSizeH(){
        return (int)(imgSizeH*fcv.getProportion());
    }

    public void registerMapAni(String name, int level, int pngSizeW, int pngSizeH, ArrayList<String> imgUrls, int visiable, int needPlayTime){
        aName = name;
        mapLevel = level;
        this.imgSizeW = pngSizeW;
        this.imgSizeH = pngSizeH;
        animatImgUrls = imgUrls;
        this.visiable = visiable;
        Gson gson = new Gson();
        System.out.println(gson.toJson(this));
        animatImgs.clear();
        this.needPlayTime = needPlayTime;
        for(String url: animatImgUrls){
            animatImgs.add(Toolkit.getDefaultToolkit().getImage(url));
        }
    }


    public Image getAnimationImg(){
        return animatImgs.get(showIndex);
    }

    public void resetAnimation(){
        showIndex = 0;
        curPlayTimes = 0;
    }

    public void triggleChange(){
        if(showIndex + 1 >= animatImgs.size()){
            showIndex = 0;
            if(needPlayTime != -1){
                //不等-1那就是要计算次数
                curPlayTimes++;
                if(curPlayTimes >= needPlayTime){
                    //TODO 已经播放达到次数了,停止播放并且隐藏
                    visiable = 0;
                    curPlayTimes = 0;
                }
            }
        } else {
            showIndex++;
        }
    }
}
